#!/bin/bash

echo "===== 智能地址解析服务部署脚本 ====="
echo ""

# 默认设置
ENV_FILE=".env"
COMPOSE_FILE="docker-compose.prod.yml"

# 检查环境文件是否存在
function check_env_file {
    if [ ! -f "$ENV_FILE" ]; then
        echo "警告: 环境配置文件 $ENV_FILE 不存在!"
        read -p "是否继续? [y/N]: " continue_without_env
        
        if [[ ! "$continue_without_env" =~ ^[Yy]$ ]]; then
            echo "操作已取消"
            return 1
        fi
    fi
    return 0
}

function show_menu {
    echo "请选择操作:"
    echo "1. 启动生产环境"
    echo "2. 查看日志"
    echo "3. 停止生产环境"
    echo "4. 重建生产环境 (完全重建镜像)"
    echo "5. 配置环境设置"
    echo "6. 进入容器Shell"
    echo "7. 退出"
    echo ""
}

function start_env {
    echo ""
    if check_env_file; then
        echo "正在启动生产环境..."
        ENV_FILE=$ENV_FILE docker compose -f $COMPOSE_FILE up -d
        echo ""
        # 获取容器IP
        CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' address-parser-prod 2>/dev/null || echo "localhost")
        PORT=$(grep "^PORT=" $ENV_FILE 2>/dev/null | cut -d= -f2 || echo "5000")
        echo "生产环境已启动! 请访问 http://localhost:$PORT"
        echo ""
    fi
}

function view_logs {
    echo ""
    echo "显示容器日志 (按Ctrl+C退出日志查看)..."
    docker compose -f $COMPOSE_FILE logs -f
    echo ""
}

function stop_env {
    echo ""
    echo "正在停止生产环境..."
    docker compose -f $COMPOSE_FILE down
    echo ""
    echo "生产环境已停止!"
    echo ""
}

function rebuild_env {
    echo ""
    if check_env_file; then
        echo "正在重建生产环境 (这可能需要几分钟)..."
        ENV_FILE=$ENV_FILE docker compose -f $COMPOSE_FILE down
        ENV_FILE=$ENV_FILE docker compose -f $COMPOSE_FILE build --no-cache
        ENV_FILE=$ENV_FILE docker compose -f $COMPOSE_FILE up -d
        echo ""
        PORT=$(grep "^PORT=" $ENV_FILE 2>/dev/null | cut -d= -f2 || echo "5000")
        echo "生产环境已重建并启动! 请访问 http://localhost:$PORT"
        echo ""
    fi
}

function container_shell {
    echo ""
    CONTAINER=$(docker compose -f $COMPOSE_FILE ps -q address-parser 2>/dev/null)
    
    if [ -z "$CONTAINER" ]; then
        echo "容器未运行，无法进入Shell"
        return
    fi
    
    echo "正在进入容器Shell..."
    docker exec -it $CONTAINER /bin/bash || docker exec -it $CONTAINER /bin/sh
    echo ""
}

function configure_env {
    echo ""
    echo "环境设置:"
    echo "1. 使用生产环境 (docker-compose.prod.yml)"
    echo "2. 使用开发环境 (docker-compose.yml)"
    echo "3. 选择自定义环境文件"
    echo "4. 返回主菜单"
    
    read -p "请选择 (1-4): " env_choice
    
    case $env_choice in
        1)
            COMPOSE_FILE="docker-compose.prod.yml"
            echo "已选择生产环境配置"
            ;;
        2)
            COMPOSE_FILE="docker-compose.yml"
            echo "已选择开发环境配置"
            ;;
        3)
            read -p "请输入环境文件路径: " custom_env
            if [ -f "$custom_env" ]; then
                ENV_FILE="$custom_env"
                echo "已设置环境文件: $ENV_FILE"
            else
                echo "错误: 文件 $custom_env 不存在"
            fi
            ;;
        4)
            return
            ;;
        *)
            echo "无效选项"
            ;;
    esac
    echo ""
}

# 主循环
while true; do
    show_menu
    read -p "请输入选项 (1-7): " choice
    
    case $choice in
        1) start_env ;;
        2) view_logs ;;
        3) stop_env ;;
        4) rebuild_env ;;
        5) configure_env ;;
        6) container_shell ;;
        7) echo -e "\n感谢使用! 再见!\n"; exit 0 ;;
        *) echo -e "\n无效选项，请重新输入!\n" ;;
    esac
done 